package com.dodoke.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dodoke.dto.RetailStoreDTO;
import com.dodoke.entity.RetailServiceCenter;
import com.dodoke.entity.RetailStore;
import com.dodoke.service.IRetailStoreService;
import com.dodoke.utils.AjaxResult;
import com.dodoke.utils.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 门店管理控制层
 */
@Slf4j
@RestController
@RequestMapping("/retail/store")
public class RetailStoreController {
    @Autowired
    private IRetailStoreService retailStoreService;

    /**
     * 门店管理分页请求接口
     *
     * @param currentPage    当前页码
     * @param pageSize       每页显示数量
     * @param retailStoreDTO 模糊查询条件
     * @return
     */
    @GetMapping("/{currentPage}/{pageSize}")
    public AjaxResult getListByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize, RetailStoreDTO retailStoreDTO) {
        return AjaxResult.success("门店/网点列表请求成功", retailStoreService.getListByPage(currentPage, pageSize, retailStoreDTO));
    }

    /**
     * 新增门店/网点
     *
     * @param retailStore
     * @return
     */
    @PostMapping
    public AjaxResult addRetailStore(@Validated @RequestBody RetailStore retailStore) {
        // 判断门店/网点名称是否存在
        RetailStore retailStoreByName = retailStoreService.getOne(new LambdaQueryWrapper<RetailStore>().eq(RetailStore::getName, retailStore.getName()));
        if (retailStoreByName != null) {
            return AjaxResult.error(HttpStatus.ERROR, "门店/网点名称已存在");
        }
        // 判断门店/网点名称编号是否存在
        RetailStore retailStoreByCode = retailStoreService.getOne(new LambdaQueryWrapper<RetailStore>().eq(RetailStore::getNo, retailStore.getNo()));
        if (retailStoreByCode != null) {
            return AjaxResult.error(HttpStatus.ERROR, "门店/网点编号已存在");
        }
        int count = retailStoreService.saveRetailStore(retailStore);
        if (count <= 0) {
            return AjaxResult.error(HttpStatus.ERROR, "门店/网点添加失败");
        }
        return AjaxResult.success("新增门店/网点成功");
    }

    /**
     * 编辑门店/网点
     *
     * @param retailStore
     * @return
     */
    @PutMapping
    public AjaxResult editRetailStore(@Validated @RequestBody RetailStore retailStore) {
       //  RetailStore storeById = retailStoreService.getById(retailStore.getId());
        // 判断门店/网点名称是否存在
        RetailStore retailStoreByName = retailStoreService.getOne(new LambdaQueryWrapper<RetailStore>().eq(RetailStore::getName, retailStore.getName()));
        if (retailStoreByName != null && !retailStoreByName.getId().equals(retailStore.getId())) {
            return AjaxResult.error(HttpStatus.ERROR, "门店/网点名称已存在");
        }
        // 判断门店/网点名称编号是否存在
        RetailStore retailStoreByCode = retailStoreService.getOne(new LambdaQueryWrapper<RetailStore>().eq(RetailStore::getNo, retailStore.getNo()));
        if (retailStoreByCode != null && !retailStoreByCode.getId().equals(retailStore.getId())) {
            return AjaxResult.error(HttpStatus.ERROR, "门店/网点编号已存在");
        }
        int count = retailStoreService.editRetailStore(retailStore);
        if (count <= 0) {
            return AjaxResult.error(HttpStatus.ERROR, "门店/网点编辑失败");
        }
        return AjaxResult.success("编辑门店/网点成功");
    }

    /**
     * 根据id获取门店/网点信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        return AjaxResult.success("获取门店/网点信息成功", retailStoreService.getById(id));
    }

    /**
     * 根据id删除取门店/网点信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable Long id) {
        boolean isRemove = retailStoreService.removeById(id);
        return isRemove ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }
}

